package com.candy.test;

import com.candy.dao.BlogMapper;
import com.candy.dao.UserInfoMapper;
import com.candy.pojo.Blog;
import com.candy.pojo.UserInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DBDriver {
    public static void main(String[] args) {
        test11();
    }


    public static void test11() {
        try {
            String resource = "mybatis-config.xml";
//            读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            创建SqlSessionFactory实例
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           SqlSession相当于JDBC中的statement，用来执行具体的增删改查的
            SqlSession session = sqlSessionFactory.openSession();
            UserInfoMapper mapper=   session.getMapper(UserInfoMapper.class);
            HashMap<String,Object> map=new HashMap<>();
            map.put("tom","123456");
            map.put("红桃A","111111");
            map.put("黑桃6","222222");
            int num=mapper.insertUserInfoByMap(map);
            System.out.println(num);
            session.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void test10() {
        try {
            String resource = "mybatis-config.xml";
//            读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            创建SqlSessionFactory实例
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           SqlSession相当于JDBC中的statement，用来执行具体的增删改查的
            SqlSession session = sqlSessionFactory.openSession();
            UserInfoMapper mapper=   session.getMapper(UserInfoMapper.class);

            List<UserInfo> userinfoes= mapper.selectUserByParams2("张","青");
            for (UserInfo user:userinfoes
            ) {
                System.out.println(user);
            }
            session.commit();
            System.out.println("==================二次查询操作=================");
            UserInfoMapper mapper2=   session.getMapper(UserInfoMapper.class);

            List<UserInfo> userinfoes2= mapper2.selectUserByParams2("张","青");
            for (UserInfo user:userinfoes2
            ) {
                System.out.println(user);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void test09() {
        try {
            String resource = "mybatis-config.xml";
//            读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            创建SqlSessionFactory实例
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           SqlSession相当于JDBC中的statement，用来执行具体的增删改查的
            SqlSession session = sqlSessionFactory.openSession();
            BlogMapper mapper=   session.getMapper(BlogMapper.class);
            Blog blog= mapper.selectBlogDetails(1);
            System.out.println(blog);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void test08() {
        try {
            String resource = "mybatis-config.xml";
//            读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            创建SqlSessionFactory实例
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           SqlSession相当于JDBC中的statement，用来执行具体的增删改查的
            SqlSession session = sqlSessionFactory.openSession();
            UserInfoMapper mapper=   session.getMapper(UserInfoMapper.class);
            UserInfo userinfo=new UserInfo();
            userinfo.setUsername("张");

            List<UserInfo> userinfoes= mapper.selectUserByParams3(userinfo,"青");
            for (UserInfo user:userinfoes
            ) {
                System.out.println(user);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void test07() {
        try {
            String resource = "mybatis-config.xml";
//            读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            创建SqlSessionFactory实例
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           SqlSession相当于JDBC中的statement，用来执行具体的增删改查的
            SqlSession session = sqlSessionFactory.openSession();
            UserInfoMapper mapper=   session.getMapper(UserInfoMapper.class);

            List<UserInfo> userinfoes= mapper.selectUserByParams2("张","青");
            for (UserInfo user:userinfoes
            ) {
                System.out.println(user);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void test05() {
        try {
            String resource = "mybatis-config.xml";
//            读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            创建SqlSessionFactory实例
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           SqlSession相当于JDBC中的statement，用来执行具体的增删改查的
            SqlSession session = sqlSessionFactory.openSession();
            UserInfoMapper mapper=   session.getMapper(UserInfoMapper.class);
            UserInfo userInfo=new UserInfo();
            userInfo.setUsername("张");
            userInfo.setUseraddress("青");
            List<UserInfo> userinfoes= mapper.selectUserInfoByLike(userInfo);
            for (UserInfo user:userinfoes
            ) {
                System.out.println(user);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void test04() {
        try {
            String resource = "mybatis-config.xml";
//            读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            创建SqlSessionFactory实例
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           SqlSession相当于JDBC中的statement，用来执行具体的增删改查的
            SqlSession session = sqlSessionFactory.openSession();
            UserInfoMapper mapper=   session.getMapper(UserInfoMapper.class);

            List<Integer> ids=new ArrayList<>();
            ids.add(1);
            ids.add(2);
            ids.add(8);
            ids.add(4);
            List<UserInfo> userinfoes= mapper.selectUserInfoByIds(ids);
            for (UserInfo user:userinfoes
            ) {
                System.out.println(user);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void test03() {
        try {
            String resource = "mybatis-config.xml";
//            读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            创建SqlSessionFactory实例
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           SqlSession相当于JDBC中的statement，用来执行具体的增删改查的
            SqlSession session = sqlSessionFactory.openSession();
            UserInfoMapper mapper=   session.getMapper(UserInfoMapper.class);
            UserInfo userInfo=new UserInfo();
            userInfo.setUsername("admin");
            userInfo.setUseraddress("青岛");

            List<UserInfo> userinfoes= mapper.selectUserInfo2(userInfo);
            for (UserInfo user:userinfoes
            ) {
                System.out.println(user);
            }




        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void test02() {
        try {
            String resource = "mybatis-config.xml";
//            读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            创建SqlSessionFactory实例
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           SqlSession相当于JDBC中的statement，用来执行具体的增删改查的
            SqlSession session = sqlSessionFactory.openSession();
            UserInfoMapper mapper=   session.getMapper(UserInfoMapper.class);
            UserInfo userInfo=new UserInfo();
//            userInfo.setUsername("admin");
//            userInfo.setUseraddress("青岛");

            List<UserInfo> userinfoes= mapper.selectUserInfo(userInfo);
            for (UserInfo user:userinfoes
                 ) {
                System.out.println(user);
            }


            System.out.println(userInfo);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static void test01() {
        try {
            String resource = "mybatis-config.xml";
//            读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            创建SqlSessionFactory实例
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           SqlSession相当于JDBC中的statement，用来执行具体的增删改查的
            SqlSession session = sqlSessionFactory.openSession();
            UserInfoMapper mapper=   session.getMapper(UserInfoMapper.class);

            UserInfo userInfo= mapper.selectByPrimaryKey(1);

            System.out.println(userInfo);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void test06() {
        try {
            String resource = "mybatis-config.xml";
//            读取配置文件
            InputStream inputStream = Resources.getResourceAsStream(resource);
//            创建SqlSessionFactory实例
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//           SqlSession相当于JDBC中的statement，用来执行具体的增删改查的
            SqlSession session = sqlSessionFactory.openSession();
            UserInfoMapper mapper=   session.getMapper(UserInfoMapper.class);
            UserInfo user=new UserInfo();
            user.setUsername("张三");
            user.setUserpwd("123456");
            user.setUseraddress("大连");
            user.setUsertel("13698745698");
           int num= mapper.insertSelective(user);
            session.commit();
            System.out.println(num);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }





}
